X-Git-Url: https://git.r.bdr.sh/rbdr/captura/blobdiff_plain/c9b9e1d654ea697afad9f6427d94623bfdf55cce..9be247c36cb15153de2cbafe7e1c53209182f29e:/Captura/Core%20Extensions/CVImageBuffer%20cgImage.swift/rbdr/captura/blobdiff_plain/c9b9e1d654ea697afad9f6427d94623bfdf55cce..9be247c36cb15153de2cbafe7e1c53209182f29e:/Captura/Core%20Extensions/CVImageBuffer+cgImage.swift diff --git a/Captura/Core Extensions/CVImageBuffer+cgImage.swift b/Captura/Core Extensions/CVImageBuffer+cgImage.swift index c30cb0c..3f59b8e 100644 --- a/Captura/Core Extensions/CVImageBuffer+cgImage.swift +++ b/Captura/Core Extensions/CVImageBuffer+cgImage.swift @@ -3,9 +3,19 @@ import ReplayKit extension CVImageBuffer { + private static let contextQueue = DispatchQueue(label: "com.example.contextQueue") + static let sharedContext: CIContext = { + return CIContext() + }() + var cgImage: CGImage? { - let ciImage = CIImage(cvImageBuffer: self) - let context = CIContext() - return context.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(self), height: CVPixelBufferGetHeight(self))) + var result: CGImage? + CVImageBuffer.contextQueue.sync { + let ciImage = CIImage(cvImageBuffer: self) + let width = CVPixelBufferGetWidth(self) + let height = CVPixelBufferGetHeight(self) + result = CVImageBuffer.sharedContext.createCGImage(ciImage, from: CGRect(x: 0, y: 0, width: width, height: height)) + } + return result } }